﻿using JewererBasic.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic.BO
{
    public class Tree_Order_BO
    {
        internal static bool CreateNewTree(List<ORDER_TREE_DETAIL> models, out int id)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    // Add Tree_Order
                    MSQDBEntities db = new MSQDBEntities();
                    ORDER_TREE tree = new ORDER_TREE();
                    tree.DateMake = DateTime.Now;
                    tree.MC = CurrentUser.UserID;
                    tree.Finish = -1;// has not start
                    db.ORDER_TREE.Add(tree);
                    db.SaveChanges();

                    id = tree.Order_Tree_Id;

                    // Add Tree_Order_Detail
                    for (int i = 0; i < models.Count; i++)
                    {
                        models[i].Order_Tree_Id = tree.Order_Tree_Id;
                        models[i].Finish = -1;
                        models[i].ReturnWeight = 0;
                        models[i].RejectWeight = 0;
                        db.ORDER_TREE_DETAIL.Add(models[i]);
                    }
                    db.SaveChanges();
                    scope.Complete();
                    return true;
                }catch(Exception ex)
                {
                    id = -1;
                    return false;
                }
            }
        }

        internal static bool UpdateTree(List<ORDER_TREE_DETAIL> models, int id)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    // Add Tree_Order
                    MSQDBEntities db = new MSQDBEntities();

                    // Add Tree_Order_Detail
                    for (int i = 0; i < models.Count; i++)
                    {
                        var detailInDB=db.ORDER_TREE_DETAIL.Find(models[i].Order_Tree_Detail_Id);
                        if(detailInDB!=null){
                            
                        }
                    }
                    db.SaveChanges();
                    scope.Complete();
                    return true;
                }catch(Exception ex)
                {
                    return false;
                }
            }
        }

        internal static ORDER_TREE GetTree(int id)
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.ORDER_TREE.Find(id);
        }

        internal static void DeleteTreeDetail(int idDetail)
        {
            MSQDBEntities db = new MSQDBEntities();
            var detail = db.ORDER_TREE_DETAIL.Find(idDetail);
            if (detail != null)
            {
                db.ORDER_TREE_DETAIL.Remove(detail);
                db.SaveChanges();
            }
        }

        internal static ORDER_TREE_DETAIL FindTreeDetail(int idDetail)
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.ORDER_TREE_DETAIL.Find(idDetail);
        }

        internal static void UpdateDetail(ORDER_TREE_DETAIL model)
        {
            MSQDBEntities db = new MSQDBEntities();
            var detailDB = db.ORDER_TREE_DETAIL.Find(model.Order_Tree_Detail_Id);
            if (detailDB != null)
            {
                detailDB.Component_Id = model.Component_Id;
                detailDB.ExportWeight = Math.Round(model.ExportWeight, 3);
                detailDB.RejectWeight = 0;
                detailDB.ReturnWeight = 0;
                detailDB.Finish = -1;
                detailDB.ListBag = model.ListBag;
                detailDB.WorkerId = model.WorkerId;
                db.SaveChanges();
            }
        }

        internal static void UpdateReturn(ORDER_TREE_DETAIL model)
        {
            MSQDBEntities db = new MSQDBEntities();
            var detailDB = db.ORDER_TREE_DETAIL.Find(model.Order_Tree_Detail_Id);
            if (detailDB != null)
            {
                detailDB.ReturnWeight += model.ReturnWeight;
                detailDB.RejectWeight += model.RejectWeight;
                detailDB.ReturnWeight = Math.Round(detailDB.ReturnWeight, 3);
                detailDB.RejectWeight = Math.Round(detailDB.RejectWeight, 3);
                db.SaveChanges();

                // add weight to component
                COMPONENT com = db.COMPONENTs.Find(model.Component_Id);
                if (com != null)
                {
                    com.Weight += model.ReturnWeight;
                    if (com.Weight.HasValue)
                    {
                        com.Weight = Math.Round(com.Weight.Value, 3);
                    }
                    if (com.Dust.HasValue)
                    {
                        com.Dust = Math.Round(com.Dust.Value + model.RejectWeight, 3);
                    }
                    else
                    {
                        com.Dust = Math.Round(model.RejectWeight, 3);
                    }
                    db.SaveChanges();
                }
            }
        }

        internal static void FinishDetail(ORDER_TREE_DETAIL model)
        {
            MSQDBEntities db = new MSQDBEntities();
            var detailDB = db.ORDER_TREE_DETAIL.Find(model.Order_Tree_Detail_Id);
            if (detailDB != null)
            {
                detailDB.Finish = 1;// finish
                detailDB.ReturnWeight += model.ReturnWeight;
                detailDB.RejectWeight += model.RejectWeight;
                detailDB.ReturnWeight = Math.Round(detailDB.ReturnWeight, 3);
                detailDB.RejectWeight = Math.Round(detailDB.RejectWeight, 3);

                // Add to waste report
                Waste_Report_BO.Update_Worker_Waste(detailDB.WorkerId, detailDB);

                // cong trong bang component
                COMPONENT com = db.COMPONENTs.Find(model.Component_Id);
                if (com != null)
                {
                    com.Weight += model.ReturnWeight;
                    if (com.Weight.HasValue)
                    {
                        com.Weight = Math.Round(com.Weight.Value, 3);
                    }
                    if (com.Dust.HasValue)
                    {
                        com.Dust = Math.Round(com.Dust.Value + model.RejectWeight, 3);
                    }
                    else
                    {
                        com.Dust = Math.Round(model.RejectWeight, 3);
                    }
                    db.SaveChanges();
                }

                db.SaveChanges();
            }
        }

        internal static bool ExportDetail(int detailId)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    MSQDBEntities db = new MSQDBEntities();
                    ORDER_TREE_DETAIL tree_detail = db.ORDER_TREE_DETAIL.Find(detailId);
                    if (tree_detail != null)
                    {
                        // update component_order_detail
                        tree_detail.Finish = 0; // start
                        db.SaveChanges();                        

                        // tru trong bang component
                        COMPONENT com = db.COMPONENTs.Find(tree_detail.Component_Id);
                        if (com != null)
                        {
                            if (com.Weight.HasValue)
                            {
                                com.Weight = Math.Round((com.Weight.Value - tree_detail.ExportWeight), 3);
                            }
                            db.SaveChanges();
                        }
                        else
                            return false;

                        scope.Complete();
                        return true;
                    }
                    return false;
                }
                catch
                {
                    return false;
                }
            }
        }

        internal static List<ORDER_TREE> GetAllTree()
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.ORDER_TREE.ToList();
        }
    }
}